package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.dao.ProductMapper;
import com.mall.dao.ProductVOMapper;
import com.mall.entity.ProductVO;
import com.mall.service.ProductService;
import com.mall.vo.PageVO;
import com.mall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductVOMapper productVOMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public ResultVO selectProductByCategoryId(Integer categoryId, Integer pageNum, Integer limit) {
        Integer start=(pageNum-1)*limit;
        List<ProductVO> productVOS = productVOMapper.selectProductByCategoryId(categoryId, start, limit);
        //查询总数
        QueryWrapper<ProductVO> q = new QueryWrapper<>();
        q.eq("category_id", categoryId);
        Integer count = productVOMapper.selectCount(q);
        Integer pageCount=0;
        if (count%pageNum==0){
            pageCount=count/pageNum;
        }else {
            pageCount=count/pageNum+1;

        }


        PageVO<ProductVO> pageVO = new PageVO<>(count, pageCount, productVOS);
        return ResultVO.success(pageVO);
    }

    @Override
    public ResultVO selectProductBrandByCategoryId(Integer categoryId) {
        ArrayList<String> brands = productVOMapper.selectProductBrandByCategoryId(categoryId);
        return ResultVO.success(brands);
    }

    @Override
    public ResultVO selectProductByKeyword(String keyword, Integer pageNum, Integer limit) {
        Integer start=(pageNum-1)*limit;

        QueryWrapper<ProductVO> q = new QueryWrapper<>();
        q.like("product_name", keyword);
        Integer count = productVOMapper.selectCount(q);
        System.out.println(count+"==================================");
        Integer pageCount=0;
        if (count%pageNum==0){
            pageCount=count/pageNum;
        }else {
            pageCount=count/pageNum+1;

        }
        List<ProductVO> productVOS=productVOMapper.selectProductByKeyword(keyword,start,limit);
        PageVO<ProductVO> pageVO = new PageVO<>(count, pageCount, productVOS);

        return ResultVO.success(pageVO);
    }

    @Override
    public ResultVO selectBrandByKeyword(String keyword) {
        ArrayList<String> brands =productVOMapper.selectBrandByKeyword(keyword);
        return ResultVO.success(brands);
    }

    @Override
    public ResultVO selectProductInfo(String productId) {
        List<ProductVO> productInfo=productVOMapper.selectProductInfo(productId);
        return ResultVO.success(productInfo);
    }

    @Override
    public ResultVO listRecommendProducts() {
        List<ProductVO> list = productVOMapper.listRecommendProducts();
        return ResultVO.success(list);
    }
}
